//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    id: '',
    items: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    run_cash: 0,
    hide_modal: true,
    input_focus: false,
    cash: 0,
    is_host: false,
    host_name: '',
    is_over: false,
    win_side: 0,

  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  onUpWin: function (e) {
    const that = this
    wx.showModal({
      title: '提示',
      content: '确定上路胜利吗？',
      success: function (res) {
        if (res.confirm) {
          that.setData({
            win_side: 1,
            show_win_btn: false
          })
          app.request({
            url: app.root + 'round/winside',
            method: 'post',
            data: {
              id: that.data.id,
              side: 1
            }
          })
        } else if (res.cancel) {

        }
      }
    })
  },

  onDownWin: function (e) {
    const that = this
    wx.showModal({
      title: '提示',
      content: '确定下路胜利吗？',
      success: function (res) {
        if (res.confirm) {
          that.setData({
            win_side: 2,
            show_win_btn: false
          })
          app.request({
            url: app.root + 'round/winside',
            method: 'post',
            data: {
              id: that.data.id,
              side: 2
            }
          })
        } else if (res.cancel) {

        }
      }
    })
  },

  onCreateBtn: function (e) {
    this.setData({
      cash_input: '',
      hide_modal: false,
      input_focus: true
    })
  },

  onCancelBtn: function (e) {
    this.setData({
      hide_modal: true
    })
  },

  onCashInput: function (e) {
    this.data.cash_input = Number(e.detail.value) || 0
  },

  onComfirmBtn: function (e) {
    if (this.clock == true) {
      return
    }
    this.clock = true

    const that = this
    const cash = this.data.cash
    wx.showLoading({
      title: '创建中',
    })

    app.request({
      url: app.root + 'round/create',
      method: 'post',
      data: {
        cash: this.data.cash_input
      },
      success: function (res) {
        wx.hideLoading()
        if (res.data.err) {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false
          })
        } else {
          wx.reLaunch({
            url: '/pages/index/index?id=' + res.data.id,
            success: function () {
              that.clock = false
              wx.showModal({
                title: '提示',
                content: '创建成功，马上分享吧！',
                showCancel: false
              })
            }
          })
        }

      }

    })
  },

  onJoinBtn: function (e) {
    if (this.jlock == true) {
      return
    }
    this.jlock = true
    wx.showLoading({
      title: '加入中',
    })

    const that = this
    app.request({
      url: app.root + 'round/join',
      method: 'post',
      data: {
        formid: e.detail.formId,
        id: this.data.id
      },
      success: function (res) {
        that.jlock = false
        wx.hideLoading()
        if (res.data.err) {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false
          })
        } else {
          if (res.data.options) {
            const opt = res.data.options
            wx.requestPayment({
              'timeStamp': opt.timeStamp,
              'nonceStr': opt.nonceStr,
              'package': opt.package,
              'signType': 'MD5',
              'paySign': opt.paySign,
              'success': function (res) {
                wx.showModal({
                  title: '提示',
                  content: '加入成功',
                  showCancel: false
                })
              },
              'fail': function (res) {

              }
            })
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel: false
            })
          }
        }
      }
    })
  },

  onQuestion: function (e) {
    wx.showModal({
      title: '规则',
      content: '请将页面转发至微信群邀请玩家加入,满十人则显示随机分队结果,玩家按结果进入游戏分队即可,可以选择设置或不设置押金，押金游戏结束后会退回。',
      showCancel: false
    })
  },

  onReport: function(e){
    wx.reLaunch({
      url: '/pages/report/report',
    })
  },

  onLoad: function (opt) {
    this.data.id = opt.id
    const that = this
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      //更新数据
      that.setData({
        userInfo: userInfo
      })
    })
  },

  onShow: function () {
    const that = this
    if(this.data.id){
      that.renderWs()
    }else{
      app.request({
        url: app.root + 'round/create',
        success:function(res){
          console.log(res)
          that.data.id = res.data.id
          that.renderWs()
        }
      })
    }

  },

  renderWs: function () {
    const that = this
    wx.showLoading({
      title: '连接中',
    })
    app.socket({
      // recheck when websocke connected
      gid: that.data.id || 1,
      link: function (res) {
        app.request({
          url: app.root + 'round',
          method: 'post',
          data: {
            id: that.data.id
          },
          success: function (res) {
            console.log(res)
            wx.hideLoading()
            if (res.data.is_over) {
              that.setData({
                cash: res.data.cash,
                is_host: res.data.is_host,
                host_name: res.data.host_name,
                is_over: true,
                win_side: res.data.win_side,
                items: res.data.items
              })
              wx.closeSocket()
            } else {
              that.setData({
                cash: res.data.cash,
                is_host: res.data.is_host,
                host_name: res.data.host_name,
                win_side: res.data.win_side,
                items: that.createItems(res.data.items)
              })
            }
          }
        })
      },

      success: function (res) {
        console.log(res)
        if (res.type == 'update') {
          if (res.is_over) {
            that.setData({
              is_over: true,
              items: res.items
            })
            wx.closeSocket()
          } else {
            that.setData({
              items: that.createItems(res.items)
            })
          }
        }

        if (res.type == 'win_side') {
          that.setData({
            win_side: res.win_side
          })
        }

      }
    })
  },

  createItems: function (len) {
    let items = []
    const number = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十']
    for (let i = 1; i < 11; i++) {
      if (i <= len) {
        items.push({
          avatarUrl: '../../img/default.png',
          nickName: '玩家' + number[i - 1]
        })
      } else {
        items.push(i)
      }
    }

    return items
  },


  onShareAppMessage: function (res) {
    return {
      title: this.data.host_name + '发起的5v5随机分队',
      path: '/pages/index/index?id=' + this.data.id,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})
